package com.jsgt.greekfire.ui.mine.user.address

import android.app.Activity
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.fhx.core.base.BaseActivity
import com.fhx.core.goToActivity
import com.fhx.core.utils.ToastUtils
import com.jsgt.greekfire.R
import com.jsgt.greekfire.SCAN_RESULT
import com.jsgt.greekfire.ui.mine.user.address.adapter.AddressAdapter
import com.jsgt.greekfire.ui.mine.user.address.addcontactperson.AddContactPersonActivity
import com.jsgt.greekfire.ui.mine.coindetail.transfer.TransferActivity
import com.jsgt.greekfire.common.widget.LoadPageHolder
import com.jsgt.greekfire.dao.model.ContactAddress
import com.jsgt.greekfire.dao.viewmodel.AddressViewModel
import com.jsgt.greekfire.ui.dialog.AddressClickDialog
import kotlinx.android.synthetic.main.activity_address_list.*
import kotlinx.android.synthetic.main.recyclerview.recyclerview

/**
 * 地址簿
 * author fhx
 * create time 2020/4/23 9:14
 **/
class AddressActivity : BaseActivity() {
    private val addressViewModel: AddressViewModel by lazy {
        ViewModelProvider(this).get(AddressViewModel::class.java)
    }
    private val clickDialog: AddressClickDialog by lazy {
        AddressClickDialog().copyListener {
            val cm = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
            val mClipData =
                ClipData.newPlainText("Label", addressAdapter.getItem(clickPosition).address)
            cm.setPrimaryClip(mClipData)
            ToastUtils.show(R.string.copy_success)
        }.editListener {
            goToActivity<AddContactPersonActivity> {
                putExtra("address", addressAdapter.getItem(clickPosition))
            }
        }
    }
    private var clickPosition: Int = 0

    private val addressAdapter: AddressAdapter by lazy {
        AddressAdapter()
    }

    private lateinit var loadPageHolder: LoadPageHolder

    companion object {
        const val PAGE_TYPE = "page"
        const val USER = 0
        const val TRANSFER = 1
        fun start(context: Context, pageType: Int, requestCode: Int) {
            val intent = Intent(context, AddressActivity::class.java)
            intent.putExtra(PAGE_TYPE, pageType)
            (context as Activity).startActivityForResult(intent, requestCode)
        }

        fun start(context: Context, pageType: Int) {
            val intent = Intent(context, AddressActivity::class.java)
            intent.putExtra(PAGE_TYPE, pageType)
            context.startActivity(intent)
        }
    }

    override fun needFitsSystemWindows(): Boolean {
        return false
    }

    override fun getLayoutId(): Int = R.layout.activity_address_list

    override fun main() {
        setStatusBar(R.color.transparent, false, R.color.white)
        title = getString(R.string.address_book)
        if (intent.getIntExtra(PAGE_TYPE, USER) != TRANSFER) {
            setRightImage(R.mipmap.ic_wallet_manager_add_wallet)
                .setOnClickListener(ocl)
        }
        recyclerview.layoutManager = LinearLayoutManager(this)
        addressAdapter.setOnItemClickListener { _, _, position ->
            if (intent.getIntExtra(PAGE_TYPE, USER) == TRANSFER) {
                val intent = Intent()
                intent.putExtra("address", addressAdapter.getItem(position).address)
                setResult(Activity.RESULT_OK, intent)
                finish()
            } else {
                clickPosition = position
                clickDialog.show(supportFragmentManager, "clickDialog")
            }
        }
        loadPageHolder = LoadPageHolder(this, title_layout)
        loadPageHolder.setEmptyViewText(getString(R.string.add_one))
        loadPageHolder.setEmptyViewImg(0)
        loadPageHolder.setEmptyButton(getString(R.string.add), ocl)
        loadPageHolder.setLoadState(LoadPageHolder.LoadState.LOADING)
        recyclerview.adapter = addressAdapter

        addressViewModel.getAllAddress()?.observe(this, Observer {
            addressAdapter.setNewInstance(it as MutableList<ContactAddress>?)
            if (it == null || it.isEmpty()) {
                loadPageHolder.setLoadState(LoadPageHolder.LoadState.EMPTY)
            } else {
                loadPageHolder.setLoadState(LoadPageHolder.LoadState.SUCCESS)
            }
        })
    }

    private val ocl = View.OnClickListener {
        goToActivity<AddContactPersonActivity>()
    }
}